<template>
	<view>
		<block v-for="(item,index) in conData" :key="index">
			<my-set-item :item="item" @goSet="setClick"></my-set-item>
		</block>
		<my-set-item :item="jySetData" @goSet="setClick" @changeChecked="changeChecked">
			<template v-slot:titleSlot>
				<view class="title">
					<view class="top-title">
						<text>{{jySetData.title}}</text>
					</view>
					<view class="detail">{{jySetData.detail}}</view>
				</view>
			</template>
		</my-set-item>
		<my-set-item :item="qbhSetData" @goSet="setClick" @changeChecked="changeChecked">
			<template v-slot:titleSlot>
				<view class="title">
					<view class="top-title">
						<text>{{qbhSetData.title}}</text>
					</view>
					<view class="detail">{{qbhSetData.detail}}</view>
				</view>
			</template>
		</my-set-item>
	</view>
</template>

<script>
	import mySetItem from '@/components/content/my-set-item/my-set-item.vue'

	import {
		queryGroupInfo,
		changeGroupState,
		dissolutionGroup
	} from '@/api/http/msg/group.js'

	import {
		setGroupData,
		slotSetData_1,
		slotSetData_2
	} from './group.js'

	import {
		msgHomeData,
		msgChatData
	} from '@/object/msg.js'

	import {
		mapActions
	} from 'vuex'

	import {
		getNowTime
	} from '@/utils/time/time.js'

	export default {
		components: {
			mySetItem
		},
		data() {
			return {
				conData: setGroupData,
				jySetData: slotSetData_1,
				qbhSetData: slotSetData_2,

				groupInfoData: {},
				groupId: ''
			}
		},
		methods: {
			...mapActions('m_chat', ['setMsgList', 'addChatData', 'clearLocalMsgList']),
			//1，获取群聊基本信息
			queryGroupInfo(gid) {
				queryGroupInfo(gid).then(res => {
					if (res.data.pubInfo.code !== 0) {
						return uni.$showMsg(res.data.pubInfo.message);
					}
					this.groupInfoData = res.data.busInfo

					if (this.groupInfoData.estoppel == '1') {
						this.jySetData.checkSwitch = true
					} else {
						this.jySetData.checkSwitch = false
					}

					if (this.groupInfoData.protect == '1') {
						//console.log('开启群保护')
						this.qbhSetData.checkSwitch = true
					} else {
						//console.log('不开启群保护')
						this.qbhSetData.checkSwitch = false
					}
				})
			},
			//2，Switch改变
			changeChecked(item, e) {
				if (item.id == 2) {
					this.changeEstoppel(item, e)
				} else if (item.id == 3) {
					this.changeProtect(item, e)
				}
			},
			//2.1，更改群禁言模式
			async changeEstoppel(item, e) {
				//组装消息发送
				var nowTime = getNowTime()
				let msgData = {
					type: this.$chatType.ESTOPPEL,
					msg: {
						groupId: this.groupId,
						senderId: uni.getStorageSync('userId'),
						types: this.$chatType.ESTOPPEL,
						groupHead: this.groupInfoData.groupHead,
						groupName: this.groupInfoData.groupName,
						time: nowTime
					}
				}

				if (e) {
					var that = this
					uni.$isShowModal('是否开启群禁言模式，开启后仅管理员和群主可以发言！').then(resultState => {
						//console.log(resultState)
						if (resultState) {
							that.changeGroupState(that.groupInfoData.gid, '1', '')
							msgData.msg.message = '用户：' + uni.getStorageSync('nickname') + '开启全员禁言'

							uni.myWebsocket.socketTask.send({
								data: JSON.stringify(msgData),
								async success() {},
							});
							return
						} else {
							return
						}
					})

				} else {
					this.changeGroupState(this.groupInfoData.gid, '2', '')
					msgData.msg.message = '用户：' + uni.getStorageSync('nickname') + '关闭全员禁言'
				}

				//console.log(msgData)

				var that = this
				uni.myWebsocket.socketTask.send({
					data: JSON.stringify(msgData),
					async success() {},
				});

			},
			//2.2，更改群保护模式
			async changeProtect(item, e) {
				var nowTime = getNowTime()
				let msgData = {
					type: this.$chatType.ESTOPPEL,
					msg: {
						groupId: this.groupId,
						senderId: uni.getStorageSync('userId'),
						types: this.$chatType.PROTECT,
						groupHead: this.groupInfoData.groupHead,
						groupName: this.groupInfoData.groupName,
						time: nowTime
					}
				}

				if (e) {
					var that = this
					uni.$isShowModal('是否开启群保护模式？').then(resultState => {
						if (resultState) {
							that.changeGroupState(that.groupInfoData.gid, '', '1')
							msgData.msg.message = '用户：' + uni.getStorageSync('nickname') + '开启群保护模式'
							return uni.myWebsocket.socketTask.send({
								data: JSON.stringify(msgData),
								async success() {},
							});
						} else {
							return
						}
					})
				} else {
					this.changeGroupState(this.groupInfoData.gid, '', '2')
					msgData.msg.message = '用户：' + uni.getStorageSync('nickname') + '关闭群保护模式'
				}

				var that = this
				uni.myWebsocket.socketTask.send({
					data: JSON.stringify(msgData),
					async success() {},
				});

			},

			//3，更改模式
			changeGroupState(groupId, estoppel, protect) {
				changeGroupState(groupId, estoppel, protect).then(res => {
					if (res.data.pubInfo.code !== 0) {
						return uni.$showMsg(res.data.pubInfo.message);
					}
					return uni.$showMsg(res.data.busInfo);
				})
			},
			//4，不同设置
			setClick(item) {
				if (item.id == 1) {
					uni.navigateTo({
						url: '/subpkg/msg/set/set-group/set-group-admin-list/set-group-admin-list?groupId=' + this
							.groupId
					})
				} else if (item.id == 5) {
					this.dissolutionGroup()
				} else if (item.id == 4) {
					return uni.navigateTo({
						url: '/subpkg/msg/set/set-group/set-group-admin-invite/set-group-admin-invite?groupId=' +
							this.groupId
					})
				} else if (item.id == 6) {
					return uni.navigateTo({
						url: '/subpkg/msg/set/set-group/set-group-close-user/set-group-close-user?groupId=' +
							this.groupId + "&groupHead=" + this.groupInfoData.groupHead +
							"&groupName=" + this.groupInfoData.groupName
					})
				}
			},
			//5，解散群聊
			async dissolutionGroup() {
				var that = this
				uni.$isShowModal('解散该群聊？').then(resultState => {
					if (resultState) {
						var nowTime = getNowTime()

						dissolutionGroup(that.groupId).then(res => {
							if (res.data.pubInfo.code !== 0) {
								return uni.$showMsg(res.data.pubInfo.message);
							}
							//发送系统消息给各个用户
							let msgData = {
								type: that.$chatType.DISSOLUTION_GROUP,
								msg: {
									groupId: that.groupId,
									senderId: uni.getStorageSync('userId'),
									types: that.$chatType.KICK_OUT_USER,
									groupHead: that.groupInfoData.groupHead,
									groupName: that.groupInfoData.groupName,
									time: nowTime,
									message: '用户：' + uni.getStorageSync('nickname') + '已经解散了群聊：'
								}
							}
							uni.myWebsocket.socketTask.send({
								data: JSON.stringify(msgData),
								async success() {
									//清空本地记录
									var cleanMsgObject = {
										id: that.groupId,
										type: 3
									}
									that.clearLocalMsgList(cleanMsgObject)
									//跳转消息页面
									uni.switchTab({
										url: "/pages/main/message/message"
									})
								},
							});

						})
					}
				})
			}
		},
		onLoad(e) {
			this.queryGroupInfo(e.groupId)
			this.groupId = e.groupId
		}
	}
</script>

<style lang="less" scoped>
	.top-title {
		margin: 8rpx 0;
		font-size: 28rpx;
	}

	.detail {
		font-size: 22rpx;
		color: rgba(229, 229, 229, 1);
	}
</style>